package com.op.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.op.entity.Question;
import com.op.response.ResponseResult;
import com.op.service.QuestionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Question)表控制层
 *
 * @author 孤焰
 * @since 2021-10-03 16:39:56
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    /**
     * 服务对象
     */
    @Resource
    private QuestionService questionService;

    /**
     * 分页查询所有数据
     * @param page     分页对象
     * @param question 查询实体
     * @return 所有数据
     */
    @GetMapping("/page")
    public ResponseResult<Page<Question>> page(Page<Question> page, Question question) {
        if (question.getTags() != null) {
            question.setTags(question.getTags().toLowerCase());
        }
        return ResponseResult.success(questionService.page(page, new LambdaQueryWrapper<>(question).orderByDesc(Question::getStatus, Question::getModifyTime, Question::getId)));
    }

    /**
     * 查询所有数据
     * @param question 查询实体
     * @return 所有数据
     */
    @GetMapping("/selectAll")
    public ResponseResult<List<Question>> selectAll(Question question) {
        return ResponseResult.success(questionService.list(new QueryWrapper<>(question)));
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne/{id}")
    public ResponseResult<Question> selectOne(@PathVariable("id") Long id) {
        return ResponseResult.success(questionService.getById(id));
    }

    /**
     * 新增数据
     * @param question 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public ResponseResult<Boolean> insert(Question question) {
        if (question.getTags() != null) {
            question.setTags(question.getTags().toLowerCase());
        }
        return ResponseResult.success(questionService.save(question));
    }

    /**
     * 修改数据
     * @param question 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public ResponseResult<Boolean> update(Question question) {
        System.out.println(question);
        return ResponseResult.success(questionService.updateById(question));
    }

    /**
     * 修改或添加数据
     * @param question 实体对象
     * @return 修改或添加结果
     */
    @PostMapping("/saveOrUpdate")
    public ResponseResult<Boolean> saveOrUpdate(Question question) {
        if (question.getTags() != null) {
            question.setTags(question.getTags().toLowerCase());
        }
        return ResponseResult.success(questionService.saveOrUpdate(question));
    }

    /**
     * 逻辑删除数据
     * @param idList 主键集合
     * @return 删除结果
     */
    @PutMapping("/batchDelete")
    public ResponseResult<Boolean> batchDelete(@RequestParam("idList") List<Long> idList) {
        return ResponseResult.success(questionService.logicDelBatchById(idList));
    }
}

